/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-22
 * Time: 20:50
 */

class Counter2{
    int counter = 0;
    public Object locker = new Object();
    public  void increase(){
        synchronized (locker){//synchronized可以修饰任何对象 ()里面可以放任何对象,这里填写locker,即给locker对象上锁
            counter++;
        }

    }

}
public class Demo3 {


        public static void main(String[] args) throws InterruptedException {
            Counter1 counter1 = new Counter1();
            Thread thread1 = new Thread(()->{
                for (int i = 0; i < 5000; i++) {
                    counter1.increase();
                }
            },"这是线程1");

            Thread thread2 = new Thread(()->{

                for (int i = 0; i < 5000; i++) {
                    counter1.increase();
                }
            },"这是线程2");

            thread1.start();
            thread2.start();

            thread1.join();
            thread2.join();
            System.out.println(counter1.counter);
        }
    }

